import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import common.util.date.DateUtil;

/**
 * @author oven:
 * @version 创建时间：2013-5-6 上午9:20:28
 * 
 */
public class Test {
	private final static String[] hexDigits = {"0", "1", "2", "3", "4",  
	        "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};  
	public static void main(String[] args) throws IOException {
		
		String[] requireFloderList ={"Bitswarm","Controllers","Core","Entities","Exceptions","FSM","Http","Logging","Protocol","Requests","Util"};
		for(int i=0;i<requireFloderList.length;i++){
			File floder = new File(parent+requireFloderList[i]);
			listFloader(floder);
		}
	}
	
	static String parent = "E:\\workspace\\cocos2d\\cocos2dResources\\cocos2d-x-current\\sfsApi\\";
	public static void listFloader(File floader){
		boolean isFloder = floader.isDirectory();
		if(isFloder){
			String[] fileList = floader.list();
			for(int i=0;i<fileList.length;i++){
				File childFloder = new File(floader.getPath()+"/"+fileList[i]);
				listFloader(childFloder);
			}
		}else{
			String filePath = floader.getPath();
			if(filePath.endsWith(".cpp")){
				filePath = filePath.replace(parent, "../../");
				filePath = filePath.replaceAll("\\\\", "/");
				System.out.println(filePath+" \\");
			}
		}
	}
	
	public static String changeSecondToTime(long avgGameTime){
		int uni = 0;//秒
		long sec = 0;
		long min = 0;
		long hour = 0;
		while(true){
			switch(uni){
			case 0:sec = avgGameTime%60;min=avgGameTime/60;break;
			case 1:min = avgGameTime%60;hour=avgGameTime/60;break;
			}
			uni++;
			avgGameTime = avgGameTime/60;
			if(avgGameTime/60>=1){
				continue;
			}else{
				break;
			}
			
		}
		return hour+"时"+min+"分"+sec+"秒";
	}
	public static void sendTotalUserEmail() throws IOException{
		String sql = "select `RoleId`,`UserId`,`RoleName` from `kom_user_role_imp3` ";
		
		 //LinkedList<HashMap<String, Object>>  result = DbUtil.getInstance().query(sql);
		 String filePath  = "D:\\ids.txt";
		 File file = new File(filePath);
		 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		 String line = null;
		 int count = 0;
		 while((line=reader.readLine())!=null){
			 count++;
			 int index = line.indexOf("\t");
			 String sUserId = line.substring(0,index);
			 String sUserRoleId = line.substring(index,line.length()).replaceAll("\t", "");
			 System.out.println(sUserId+"|"+sUserRoleId);
			 int iUserId = Integer.parseInt(sUserId);
			 int iRoleId = Integer.parseInt(sUserRoleId);
			 
			 StringBuilder sb = new StringBuilder();
			 System.out.println("生成第:"+count);
			 
			 String toolListString = "[{\"count\":2,\"toolIndexName\":\"1jiyushibaoxiang4\"}]";
			 
			 String title = "军团战补偿";
			 String content = "8月1号军团战部分玩家出现积分和排名异常，现该问题已经修复，我们对本次影响玩家正常体验军团战深表歉意，特发此补偿，感谢大家将爆三国的理解和支持。\\n将爆三国运营团队";
			 
			 sb.append(generateEmail(iUserId,iRoleId,title,content,toolListString));
			 sb.append("\n");
			 writeToFile(sb);
		 }
	}
	
	
	public static String generateEmail(int userId ,int userRoleId,String title,String content,String toolList){
		int time = DateUtil.getNowTime();
//		
		String result = "INSERT INTO `kom_email_info` (" +
				"`SenderUserId`, `SenderUserRoleId`, `ReceiveUserId`, `ReceiveUserRoleId`, `EmailType`, `EmailTitle`, `EmailContent`, `IsOpen`, `OpenTime`, `IsDelete`, `IsSendDelete`, `DeleteTime`, `AddTime`, `ToolList`, `IsReceiveToolList`)" +
				" VALUES(" +
				"0,0,"+userId+","+userRoleId+",1," +
				"'"+title+"', " +
				"'"+content+"', 0, NULL, 0, 0, NULL, "+time+","+(toolList!=null?"'"+toolList+"'":"NULL")+", 0);";
		return result;
	}
	
	
	public static void writeToFile(StringBuilder sb)
    {
        byte[] buff=new byte[]{};
        try
        {
            buff=sb.toString().getBytes();
            FileOutputStream out=new FileOutputStream("D:\\MyData\\desk\\emails.txt",true);
            out.write(buff,0,buff.length);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
	
	/** 
     * MD5 加密 
     */  
    public static String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
  
        try {  
            messageDigest = MessageDigest.getInstance("MD5");  
  
            messageDigest.reset();  
  
            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        byte[] byteArray = messageDigest.digest();  
  
        StringBuffer md5StrBuff = new StringBuffer();  
  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            else  
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
        }  
  
        return md5StrBuff.toString();  
    }  
	
	public static String generateVisibleChannelString(int serverCondition){
		int[] channelConditions={
				0,//uc
				0,//当乐
				1,//91
				0,//极游
				0,//小米
				0,//乐豆
				0,//畅玩
				0,//电信
				0,//逗趣
				0,//飞流
				0,//91IOS
				0,//安智
				0,//机锋
				1,//多酷
				0,//豌豆
				0,//宝软
				0,//移动
				0,//华为
				0,//联想
				0,
				0,
				1,//n多
				1,//拇指
				1,//一千尺
				1,//魅族
				1,//三星
				1,//十字猫
				1,//金立
				1,//谷歌
				1,//中兴
				1,//155
				1,//17sy
				1,//琵琶
				1,//p9p9
			};
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<channelConditions.length;i++){
			sb.append(i+1);
			if(channelConditions[i]!=serverCondition){
				sb.append(":"+channelConditions[i]);
			}
			if(i!=channelConditions.length-1){
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	public static int generateString(int channel){
		int condition = -1;
		String serverList = "1,2,3,4,5,6:0";
		String[] l = serverList.split(",");
		for(int i=0;i<l.length;i++){
			String[] c = l[i].split(":");
			if(c.equals(""+channel)){
				if(c.length>1){
					condition = Integer.parseInt(c[1]);
				}
			}
		}
		return condition;
	}
	
}

